package com.fengye.algorithms.leecode.dp;

/**
 * @Author fengye
 * @Date 2023/5/21 9:57
 **/
public class UniquePathsII63 {

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int n = obstacleGrid.length;
        int m = obstacleGrid[0].length;

        int[] dp = new int[m];
        dp[0] = obstacleGrid[0][0] == 1 ? 0 : 1;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                // 有障碍，dp[j] = 0. 这条路走不通
                if(obstacleGrid[i][j] == 1) {
                    dp[j] = 0;
                    continue;
                }

                if(j - 1 >= 0 && obstacleGrid[i][j - 1] == 0) {
                    dp[j] += dp[j - 1];
                }
             }
        }

        return dp[m - 1];
    }
}

